//任务管理

//站点id
var siteid;

// 任务ID
var taskid;

// 分页索引：
var index = 1;

// 每页显示数目
var pagenum = 9;

// 总数目
var count;
// 页数
var pages;

// 操纵站点下拉框时，为任务下拉框赋相应的值
$(document).ready(function() {
	// 当操作站点下拉框时，把值赋给siteid
	$("#selectwebsite").change(function() {
		siteid = $(this).attr('value');
		displaytaskname(siteid);
		$("#selecttask").html("<option value=\"null\" >请先选择任务</option>");
	});
});

// 操作任务下拉框时，显示出相应的任务的url
$(document).ready(function() {
	// 当操作任务下拉框时，把值赋给taskid
	$("#selecttask").change(function() {
		taskid = $(this).attr('value');
		if (taskid == "null") {
			taskid = null;
			$("#nonedata").css("display", "block");
			$("#displaytaskdata").html("");
			$("#fenye").css("display","none");			
		} else {
			$("#displaytaskdata").css("display","block");
			$("#fenye").css("display","block");
			onetaskzhibiaocont(taskid, index, pagenum);
		}
	});
});

// 第一次加载时显示的分页
function fenye(count) {

	var intcount = parseInt(count);
	var numhtml = "";
	var counthtml;
	// 计数器
	var num = 10;
	if (count < pagenum) {
		pages = 1;
		numhtml = "<li><a href=\"javascript:void(0)\" >" + 1 + "</a></li>";
		counthtml = "当前第1页/共1页" + count + "条数据";
	} else {
		if (count % pagenum != 0) {

			pages = parseInt(intcount / pagenum);

			pages = pages + 1
		} else {
			pages = intcount / pagenum;
		}

		counthtml = "当前第" + index + "页/共" + pages + "页" + count + "条数据";

		pageshu = pages + 1;

		var dangqianpage = this.index;
		if (dangqianpage < num) {
			for ( var i = 1; i < num; i++) {
				if (i < pageshu) {
					if(i==this.index){
						numhtml += "<li id=\"fenyexuanzhong\">"+i+"</li>";
					}else{
					numhtml += "<li><a href=\"javascript:void(0)\" onclick=\"indexpage("+i+")\" >"+i+"</a></li>";
					}
				}

			}
		}
	}

	$("#fenyenum").html(numhtml);
	$("#dangqian_count").html(counthtml);
}

function twofenye(index) {

	var prev = index - 5;
	var next = index + 6;

	var numhtml = "";

	counthtml = "当前第" + index + "页/共" + pages + "页" + count + "条数据";

	for ( var i = prev; i < index; i++) {
		if (i > 0) {
			numhtml += "<li><a href=\"javascript:void(0)\" onclick=\"indexpage("
					+ i + ")\" >" + i + "</a></li>";
		}
	}

	for ( var a = index; a < next; a++) {
		if (a < pages + 1) {
			if(a==index){
				numhtml += "<li id=\"fenyexuanzhong\">"+i+"</li>";
			}else{
				numhtml += "<li><a href=\"javascript:void(0)\" onclick=\"indexpage("+a+")\" >"+a+"</a></li>";
			}	
		}

	}
	if (index < 3) {
		$("#shouye").css("display", "none");
		$("#prevpage").css("display", "none");
	} else {
		$("#shouye").css("display", "block");
		$("#prevpage").css("display", "block");
		$("#nextpage").css("display", "block");
		$("#lastpage").css("display", "block");
	}
	if (index < pages + 1 && index > pages - 10) {
		$("#nextpage").css("display", "none");
		$("#lastpage").css("display", "none");

	}

	$("#fenyenum").html(numhtml);
	$("#dangqian_count").html(counthtml);
}

function indexpage(index) {
	this.index = index;
	if(index<9){
		onetaskzhibiaocont(taskid, index, pagenum);
	}else{
		secondtaskzhibiaocont(taskid, index, pagenum);
	}
}

function shouye() {
	this.index = 1;
	$("#shouye").css("display", "none");
	$("#prevpage").css("display", "none");
	$("#nextpage").css("display", "block");
	$("#lastpage").css("display", "block");
	secondtaskzhibiaocont(taskid, index, pagenum);
}

function lastpage() {
	lastpage = this.pages;
	this.index = lastpage;
	$("#shouye").css("display", "block");
	$("#prevpage").css("display", "block");
	$("#nextpage").css("display", "none");
	$("#lastpage").css("display", "none");
	secondtaskzhibiaocont(taskid, index, pagenum);
}

function prevpage() {
	this.index = index - 1;
	secondtaskzhibiaocont(taskid, index, pagenum);
}

function nextpage() {
	this.index = index + 1;
	secondtaskzhibiaocont(taskid, index, pagenum);

}

// 显示任务名字
function displaytaskname(siteid) {
	var displayhtml="";
	$.ajax({
		type : "post",
		url : "tool/taskurl/gettasknamebysiteid.jsp",
		dataType : "json",
		data : {
			siteid : siteid
		},
		success : function(responseText, textStatus) {

			var jsonresult = eval(responseText);

			for ( var i = 0; i < jsonresult.length; i++) {
				displayhtml += "<option value=\"" + jsonresult[i].taskid
						+ "\" >" + jsonresult[i].taskname + "</option>";
			}
			$("#nonedata").css("display", "none");
			$("#selecttask").append(displayhtml);

		},
	})
}

// 添加指标
function addtaskzhibiao() {
	var fieldname = $("#fieldname").attr("value");
	var startfield = $("#startfield").attr("value");
	var endfield = $("#endfield").attr("value");
	
	fieldname = fieldname.replace(/(^\s*)|(\s*$)/g,"");
	startfield = startfield.replace(/(^\s*)|(\s*$)/g,"");
	endfield = endfield.replace(/(^\s*)|(\s*$)/g,"");
	if(fieldname=="" || startfield=="" || endfield==""){
		alert("有为空的选项，无法添加");
	}else{
		$.ajax({
			type : "post",
			url : "tool/taskzhibiao/addtaskzhibiao.jsp",
			dataType : "text",
			data : {
				taskid : taskid,
				fieldname : fieldname,
				startfield : startfield,
				endfield : endfield
			},
			success : function(issuccess, textStatus) {
				hideDiv('disnewtaskzhibiao');
				alert(issuccess);
				$("#fieldname").val(" ");
				$("#startfield").val(" ");
				$("#endfield").val(" ");
				if(index<9){
					onetaskzhibiaocont(taskid, index, pagenum);
				}else{
					secondtaskzhibiaocont(taskid, index, pagenum);
				}
			},
		})
	}
}
// 显示某站点某任务下的指标配置
function onetaskzhibiaocont(taskid, index, pagenum) {
	var displayhtml = "<table cellspacing=\"0\" id=\"displaytable\" ><tbody><tr><th style=\"border-left:1px solid #DBDBDB;\" width=\"25%\" >指标名称</th><th width=\"25%\">开始字段</th><th width=\"25%\">结束字段</th><th width=\"25%\">操作</th></tr>";
	$.ajax({
				type : "post",
				url : "tool/taskzhibiao/displaytaskzhibiao.jsp",
				dataType : "json",
				data:{
					taskid:taskid,
					index:index,
					pagenum:pagenum
				},
				success : function(responseText, textStatus) {

					var jsondata = eval(responseText);
					for ( var i = 0; i < jsondata.length; i++) {
						displayhtml += "<tr><td class=\"tableleftborder\">"
								+ jsondata[i].field_name
								+ "</td>"
								+ "<td>"
								+ jsondata[i].start_regex
								+ "</td>"
								+ "<td>"
								+ jsondata[i].end_regex
								+ "</td>"
								+ "<td><a href=\"javascript:void(0)\"  onclick=\"displaymodifictakszhibiao('"
								+ jsondata[i].id
								+ "')\" class=\"modif\">修改</a>&nbsp;"
								+ "<a href=\"javascript:void(0)\" onclick=\"deletetaskzhibiao('"
								+ jsondata[i].id + "')\">删除</a></td></tr>";
						if (i == jsondata.length - 1) {
							count = jsondata[i].count;
						}
					}
					displayhtml += "</tbody></table>";
					$("#nonedata").css("display", "none");
					$("#displaytaskzhibiaodata").html(displayhtml);
					if(count == null){
						count=0;
						
					}
					fenye(count);
				},
			})
}

function secondtaskzhibiaocont(taskid, index, pagenum) {
	var displayhtml = "<table cellspacing=\"0\" id=\"displaytable\" ><tbody><tr><th style=\"border-left:1px solid #DBDBDB;\" width=\"25%\" >指标名称</th><th width=\"25%\">开始字段</th><th width=\"25%\">结束字段</th><th width=\"25%\">操作</th></tr>";
	$.ajax({
				type : "post",
				url : "tool/taskzhibiao/displaytaskzhibiao.jsp",
				dataType : "json",
				data : {
					taskid : taskid,
					index : index,
					pagenum : pagenum
				},
				success : function(responseText, textStatus) {

					var jsondata = eval(responseText);
					for ( var i = 0; i < jsondata.length; i++) {
						displayhtml += "<tr><td class=\"tableleftborder\">"
								+ jsondata[i].field_name
								+ "</td>"
								+ "<td>"
								+ jsondata[i].start_regex
								+ "</td>"
								+ "<td>"
								+ jsondata[i].end_regex
								+ "</td>"
								+ "<td><a href=\"javascript:void(0)\"  onclick=\"displaymodifictakszhibiao('"
								+ jsondata[i].id
								+ "')\" class=\"modif\">修改</a>&nbsp;"
								+ "<a href=\"javascript:void(0)\" onclick=\"deletetaskzhibiao('"
								+ jsondata[i].id + "')\">删除</a></td></tr>";
						if (i == jsondata.length - 1) {
							count = jsondata[i].count;
						}
					}
					displayhtml += "</tbody></table>";
					$("#nonedata").css("display", "none");
					$("#displaytaskzhibiaodata").html(displayhtml);
					twofenye(index);
				},
			})
}

// 显示修改指标页面
function displaymodifictakszhibiao(id) {

	$.ajax({
		type : "post",
		url : "tool/taskzhibiao/displaymodifictakszhibiao.jsp",
		dataType : "text",
		data : {
			id : id,
		},
		success : function(responseText, textStatus) {
			var jsonresult = eval("(" + responseText + ")");

			$("#modiffidldname").val(jsonresult.field_name);
			$("#modificstartfield").val(jsonresult.start_regex);
			$("#modificendfield").val(jsonresult.end_regex);
			$("#modificid").val(jsonresult.id);
			popupDiv('displaymodifictaskzhibiao')

		},
	})

}

// 修改指标
function modifictaskzhibiao() {

	var id = $("#modificid").attr("value");
	var fieldname = $("#modiffidldname").attr("value");
	var startfield = $("#modificstartfield").attr("value");
	var endfield = $("#modificendfield").attr("value");
	
	fieldname = fieldname.replace(/(^\s*)|(\s*$)/g,"");
	startfield = startfield.replace(/(^\s*)|(\s*$)/g,"");
	endfield = endfield.replace(/(^\s*)|(\s*$)/g,"");
	if(fieldname=="" || startfield=="" || endfield==""){
		alert("有为空的选项，无法修改");
	}else{
		$.ajax({
			type : "post",
			url : "tool/taskzhibiao/modifictaskzhibiao.jsp",
			dataType : "text",
			data : {
				id : id,
				fieldname : fieldname,
				startfield : startfield,
				endfield : endfield
			},
			success : function(issuccess, textStatus) {
				alert(issuccess);
				hideDiv('displaymodifictaskzhibiao');
				onetaskzhibiaocont(taskid, index, pagenum)
			},
		})
	}
}
// 删除指标
function deletetaskzhibiao(id) {

	$.ajax({
		type : "post",
		url : "tool/taskzhibiao/deletetaskzhibiao.jsp",
		dataType : "text",
		data : {
			id : id
		},
		success : function(issuccess, textStatus) {
			alert(issuccess);
			if(index<9){
				onetaskzhibiaocont(taskid, index, pagenum);
			}else{
				secondtaskzhibiaocont(taskid, index, pagenum);
			}
		},
	})
}

// 显示添加任务窗口：首先判断，是否选择某站点，如未选择弹窗提示
// 选择则显示添加任务窗口
function displayaddtaskzhibiao() {
	if (siteid == null) {
		alert("请先选择站点再进行相关操作");
	} else {
		if (taskid == null) {
			alert("请先选择站点，然后选择任务后再进行相关操作");
		} else {
			popupDiv('disnewtaskzhibiao')

		}

	}
}
